//
//  DetailsViewController.m
//  GiftNote
//
//  Created by lanou3g on 15/8/11.
//  Copyright (c) 2015年 GN. All rights reserved.
//

#import "DetailsViewController.h"
#import "DetailsTableViewCell.h"
#import "DetailsHesder.h"
#import "Screen.h"
#import "Details2TableViewCell.h"
@interface DetailsViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
{
    UIView *lineView;
    UIButton *writeButton;
    UITextField *textField;
    UIButton *sendButton;
}
@property (nonatomic,assign) BOOL judgeCell;
@property (nonatomic,retain) UITableView *MytableView;
@property (nonatomic,assign) CGFloat webHight1;
@end

@implementation DetailsViewController




- (void)viewDidLoad {
    [super viewDidLoad];
   self.navigationItem.title = @"单品详情";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(rightBarButtonAction)];
    
//    self.navigationController.toolbarHidden = NO;
//    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:@"12" style:UIBarButtonItemStylePlain target:self action:@selector(barButtonItemAction)];
//    barButton.width = SCREEN_WIDTH * 1.5 / 6;
//    
//    UIBarButtonItem *barButton1 = [[UIBarButtonItem alloc] initWithTitle:@"34" style:UIBarButtonItemStylePlain target:self action:@selector(barButtonItemAction1)];
//    barButton.width = SCREEN_WIDTH * 3.5 / 6 ;
//    
//    [self setToolbarItems:[NSArray arrayWithObjects:barButton,barButton1, nil] animated:YES ];
//
    
    lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 30, SCREEN_WIDTH /2, 2)];
    lineView.backgroundColor = [UIColor redColor];

    self.MytableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _MytableView.backgroundColor = [UIColor orangeColor];
    [_MytableView registerClass:[DetailsTableViewCell class] forCellReuseIdentifier:@"cell"];
    [_MytableView registerClass:[Details2TableViewCell class] forCellReuseIdentifier:@"cell1"];
    _MytableView.delegate = self;
    _MytableView.dataSource = self;
    [self.view addSubview:_MytableView];
    _judgeCell = YES;
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(webHightAction:) name:@"webHight2" object:nil];
}
- (void)rightBarButtonAction{
    
}

- (void)barButtonItemAction{
    
}

- (void)barButtonItemAction1{
    
}

- (void)webHightAction:(NSNotification *)sender{

    if (_webHight1 != [sender.object floatValue]) {
        self.webHight1  = [sender.object floatValue];
        [self.MytableView reloadData];
    }
    NSLog(@"self.webHight1%f",self.webHight1);
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 && indexPath.row == 0) {
        return 2;
    }else{
        if (_judgeCell == YES) {
            return self.webHight1;
        }else{
            return 45;
        }
    }
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0 ) {
        return 1;
    }else{
        if (section == 1 && _judgeCell == YES) {
            return 1;
        }else{
            return 100;
        }
    }
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_judgeCell == YES) {
        DetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        
        [cell setDetailsCellModel:_detailsModel];
        return cell;
    }else{
        Details2TableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
        [cell1 setDetailsCell1Model:_detailsModel];
        return cell1;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 470;
    }else{
        return 32;
    }
    
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        static NSString *headerField = @"deader";
        DetailsHesder *header = [tableView dequeueReusableCellWithIdentifier:headerField];
        if (!header) {
            header = [[DetailsHesder alloc] initWithReuseIdentifier:headerField];
        }
        [header setDetailsHeaderModel:_detailsModel];
        return header;
  
    }else{
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(0 , 0, SCREEN_WIDTH /2 , 30);
        [button setTitle:@"图文介绍" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
        button1.frame = CGRectMake(SCREEN_WIDTH /2 ,0 , SCREEN_WIDTH /2, 30);
        [button1 setTitle:@"评论" forState:UIControlStateNormal];
        [button1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 1000;
        button1.tag = 1001;
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH , 30)];
        view.backgroundColor = [UIColor grayColor];
        
        [view addSubview:lineView];
        [view addSubview:button1];
        [view addSubview:button];
        
        return view;

    }
}


- (void)buttonAction:(UIButton *)sender{
       CGRect newFrame = lineView.frame;
    if (sender.tag == 1000) {
       newFrame.origin.x = 0;
        _judgeCell = YES;
        writeButton.frame = CGRectMake(SCREEN_WIDTH - 60 , SCREEN_HEIGHT - 60, 0, 0);
        [textField removeFromSuperview];
        [sendButton removeFromSuperview];
    }else{
        [self writeWithSend];
        writeButton.frame = CGRectMake(SCREEN_WIDTH - 60, SCREEN_HEIGHT - 60, 30, 30);
        newFrame.origin.x = SCREEN_WIDTH /2;
        _judgeCell = NO;
    }
    [UIView animateWithDuration:.2 animations:^{
        lineView.frame = newFrame;
    }];
    
    [_MytableView reloadSections:[[NSIndexSet alloc] initWithIndex:1] withRowAnimation:UITableViewRowAnimationAutomatic];
}



- (void)setDetailsModel:(ArtiollerModel *)detailsModel{
    _detailsModel = detailsModel;
}
#warning 输入相关
- (void) writeWithSend{
    writeButton = [UIButton buttonWithType:UIButtonTypeSystem];
    writeButton.backgroundColor = [UIColor orangeColor];
    [writeButton addTarget:self action:@selector(writeAction:) forControlEvents: UIControlEventTouchUpInside];
    
    textField = [[UITextField alloc] initWithFrame:CGRectMake(10, SCREEN_HEIGHT, SCREEN_WIDTH - 10 - 45, 30)];
    textField.delegate = self;
    textField.backgroundColor = [UIColor whiteColor];
    //确认输入
    sendButton = [UIButton buttonWithType:UIButtonTypeSystem];
    sendButton.frame = CGRectMake(SCREEN_WIDTH - 40 + 3, SCREEN_HEIGHT, 30, 30);
    [sendButton setTitle:@"发送" forState:UIControlStateNormal];
    [sendButton addTarget:self action:@selector(sendButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:sendButton];
    [self.view addSubview:textField];
    [self.view addSubview:writeButton];
}


- (void)writeAction:(UIButton *)sender{
    CGRect newFrame = textField.frame;
    newFrame.origin.y = SCREEN_HEIGHT - 290;
    CGRect newFrame1 = sendButton.frame;
    newFrame1.origin.y = SCREEN_HEIGHT - 290;
    //回收键盘
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction:)];
    [_MytableView addGestureRecognizer:tapGestureRecognizer];
    //弹出键盘
    [textField becomeFirstResponder];
    [UIView animateWithDuration:.2 animations:^{
        textField.frame = newFrame;
        sendButton.frame = newFrame1;
    }];
}

- (void)tapGestureAction:(UITapGestureRecognizer *)sender{
    
    [textField resignFirstResponder];
    [UIView animateWithDuration:.2 animations:^{
        textField.frame = CGRectMake(10, SCREEN_HEIGHT, SCREEN_WIDTH - 10 - 45, 30);
        sendButton.frame = CGRectMake(SCREEN_WIDTH - 45, SCREEN_HEIGHT, 30, 30);
    }];
}
- (void)sendButtonAction:(UIButton *)sender{
    
    NSLog(@"发送了一条评论");
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
